<?php

namespace app\mobile\controller;
ini_set("display_errors", "on");
include "./vendor/api_sdk/vendor/autoload.php";

use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;

Config::load();

class Sms{

    static $acsClient = null;

    public static function getAcsClient() {
        //产品名称:云通信短信服务API产品,开发者无需替换
        $product = "Dysmsapi";
        //产品域名,开发者无需替换
        $domain = "dysmsapi.aliyuncs.com";
        // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
        $accessKeyId = "LTAI1hkljTFXBaFF"; // AccessKeyId
        $accessKeySecret = "y1BtFzm5YxlvK8th4J64tAeX6mZ6xx"; // AccessKeySecret
        // 暂时不支持多Region
        $region = "cn-hangzhou";
        // 服务结点
        $endPointName = "cn-hangzhou";
        if(static::$acsClient == null) {
            //初始化acsClient,暂不支持region化
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
            // 增加服务结点
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
            // 初始化AcsClient用于发起请求
            static::$acsClient = new DefaultAcsClient($profile);
        }
        return static::$acsClient;
    }

    public static function sendSms($phone, $code, $state, $type, $con){
        /* $state 1 是注册（SMS_115585143） 2 是登录（SMS_115585145） 3 是绑定手机号（SMS_204976190） */

        if($type == 1){
            switch ($state){
                case 1: $template = "SMS_115585143"; break;
                case 2: $template = "SMS_115585145"; break;
                case 3: $template = "SMS_204976190"; break;
            }
        }else{
            switch ($state){
                case 1: $template = "SMS_205396956"; $phone = $con.$phone; break;
                case 2: $template = "SMS_205407044"; $phone = $con.$phone; break;
                case 3: $template = "SMS_205396957";$phone = $con.$phone;  break;
            }
        }

        $request = new SendSmsRequest();
        $request->setPhoneNumbers("$phone");
        $request->setSignName("三生藏古董");
        $request->setTemplateCode($template);
        $request->setTemplateParam(json_encode(array(
            "code" => $code
        ), JSON_UNESCAPED_UNICODE));
        $request->setOutId(time()."_".$phone);
        // 发起访问请求
        return static::getAcsClient()->getAcsResponse($request);
    }

}